import imapclient
import datetime
from imapclient import IMAPClient

# imapObj = imapclient.IMAPClient('imap.163.com', ssl=True)
# imapObj.id_({"name": "IMAPClient", "version": "0.1.0", "vendor": "walker"})
# imapObj.login('18620358128@163.com', 'development73')

# select_info = imapObj.select_folder('INBOX', readonly=True)
# print('%d messages in INBOX' % select_info[b'EXISTS'])

# messages = imapObj.search(u'FROM 18620358128')
# print('%d message found' % len(messages))


# UIDs = imapObj.search()
# print(UIDs)
# imapObj.logout()

# context manager ensures the session is cleaned up
with IMAPClient(host="imap.163.com") as client:
    client.id_({"name": "IMAPClient", "version": "0.1.0", "vendor": "walker"})
    client.login('18620358128@163.com', 'development73')
    client.select_folder('INBOX')

    # search criteria are passed in a straightforward way
    # (nesting is supported)
    messages = client.search(['NOT', 'DELETED'])

    # fetch selectors are passed as a simple list of strings.
    response = client.fetch(messages, ['ENVELOPE'])

    # `response` is keyed by message id and contains parsed,
    # converted response items.
    for message_id, data in response.items():
        print('{id}: {envelope}'.format(
            id=message_id,
            envelope=data[b'ENVELOPE']))
